package datasv.courseProduce_datasv;

import data.datafactory.DatabaseOperator;
import po.CoursePublishPO;
import resultmessage.CourseProduceRM;

public class AddCoursePublish {
	public static void main(String[] args) {
		System.out.println(new AddCoursePublish()
				.addCoursePublish(new CoursePublishPO("00106130020",
						"test.t.t.t", "hege", "xian 2")));
	}

	protected static String[] columnName = { "id", "teacher", "time",
			"location" };
	protected static int columnNum = columnName.length;

	public CourseProduceRM addCoursePublish(CoursePublishPO po) {
		return addInSql(po);
	}

	private CourseProduceRM addInSql(CoursePublishPO po) {
		String collegeID = po.getCollegeID();
		DatabaseOperator op = new DatabaseOperator("college" + collegeID);
		if (!op.ifDatabaseExist()
				|| !op.ifIDExist("course_list", po.getCourseID())) {
			return CourseProduceRM.COURSEPUBLISH_EXIT;
		}
		int num = columnNum;
		String[][] info = new String[num][2];
		info[0][0] = columnName[0];
		info[0][1] = po.getCourseID();
		info[1][0] = columnName[1];
		info[1][1] = po.getTeacher();
		info[2][1] = po.getTime();
		info[2][0] = columnName[2];
		info[3][1] = po.getLocation();
		info[3][0] = columnName[3];

		for (int i = 0; i < num; i++) {
			try {
				op.getOneInform(po.getCollegeID(), "course_list", info[i][0])
						.equals(null);
				return CourseProduceRM.COURSEPUBLISH_EXIT;
			} catch (NullPointerException e) {
				if (!op.setOneInform(info[0][1], info[i][1], "course_list",
						info[i][0])) {
					return CourseProduceRM.COURSEPUBLISH_EXIT;
				}
				System.out.println(info[i][1]);
			}
		}
		return CourseProduceRM.SUCCESS;
	}

}
